package logic

import (
	"context"
	"github.com/jinzhu/copier"
	"xialou_platform/app/requestx"

	"xialou_platform/services/agent/rpc/agentClient"
	"xialou_platform/services/agent/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AgentListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAgentListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AgentListLogic {
	return &AgentListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 代理商列表
func (l *AgentListLogic) AgentList(in *agentClient.AgentListReq) (*agentClient.AgentListResp, error) {
	count := l.svcCtx.AgentModel.FindListCount(l.ctx, in.Keyword, in.Status)

	res := make([]*agentClient.AgentParam, 0)
	if count > 0 {
		list, err := l.svcCtx.AgentModel.FindList(l.ctx, &requestx.ListReq{
			Page:     in.Page,
			PageSize: in.PageSize,
			Keyword:  in.Keyword,
		}, in.Status)
		if err != nil {
			return nil, err
		}
		for _, agent := range list {
			temp := &agentClient.AgentParam{}
			copier.Copy(&temp, agent)
			temp.CreateTime = agent.CreateTime.Unix()
			res = append(res, temp)
		}
	}

	return &agentClient.AgentListResp{
		Total: count,
		List:  res,
	}, nil
}
